package com.github.permissiondog.cancanpandroid.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.github.permissiondog.cancanpandroid.R;
import com.github.permissiondog.cancanpandroid.model.Application;
import com.github.permissiondog.cancanpandroid.repository.api.response.ApplicationInfoResponse;
import com.github.permissiondog.cancanpandroid.repository.impl.ApplicationRepositoryImpl;
import com.github.permissiondog.cancanpandroid.ui.view.ApplicationProgressActivity;
import com.github.permissiondog.cancanpandroid.ui.viewholder.ApplicantApplicationViewHolder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ApplicantApplicationAdapter extends RecyclerView.Adapter<ApplicantApplicationViewHolder> {
    private List<Application> applications = List.of(new Application());
    private final Map<Integer, ApplicationInfoResponse> applicationInfoMap = new HashMap<>();
    private final Context parent;
    public ApplicantApplicationAdapter(Context activity) {
        this.parent = activity;
    }
    @NonNull
    @Override
    public ApplicantApplicationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        var itemView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.item_application, parent, false
        );
        Log.d("ApplicationAdapter", "onCreateViewHolder");

        flush();
        return new ApplicantApplicationViewHolder(itemView);
    }

    private void setApplications(List<Application> apps) {
        Log.d("ApplicationAdapter", "请求数据");
        applications = apps;
        for (int i = 0; i < apps.size(); i++) {
            var app = apps.get(i);
            int finalI = i;
            ApplicationRepositoryImpl.getInstance().getApplicationInfo(app.getId(), info -> {
                applicationInfoMap.put(info.getApplication().getId(), info);
                notifyItemChanged(finalI);
            });
        }
    }
    public void flush() {
        ApplicationRepositoryImpl.getInstance().getApplicantApplications(this::setApplications);
    }

    @Override
    public void onBindViewHolder(@NonNull ApplicantApplicationViewHolder holder, int position) {
        var app = applications.get(position);
        Log.d("ApplicationAdapter", "绑定视图: " + app.getId());
        var appInfo = applicationInfoMap.get(app.getId());
        if (appInfo == null)  {
            return;
        }
        holder.name.setText(appInfo.getTemplate().getTableName());
        holder.status.setText(appInfo.getStatus().getDisplayName());
        holder.creationTime.setText(appInfo.getApplication().getCreationTime());
        holder.showProgress.setOnClickListener(l -> {
            var intent = new Intent(parent, ApplicationProgressActivity.class);
            intent.putExtra("applicationId", app.getId());
            parent.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        Log.d("ApplicationAdapter", "获取元素数量: " + applications.size());
        return applications.size();
    }
}
